var ScrambledEggs;
(function (ScrambledEggs) {
    var Cooldowns = (function () {
        function Cooldowns() { }
        Cooldowns.AutoResolve = function AutoResolve(gameState) {
            this._gameState = gameState;
            for(var selector in gameState.State.Cooldowns) {
                this.Initialize(selector);
            }
            setInterval(function () {
                var me = this;
                for(var selector in gameState.State.Cooldowns) {
                    var cooldown = --gameState.State.Cooldowns[selector];
                    if(me._locked) {
                        return;
                    }
                    $(selector).each(function () {
                        var $el = $(this);
                        var origHtml = $el.attr("data-orightml");
                        if(cooldown == 0) {
                            $el.html(origHtml).removeAttr("disabled").removeAttr("data-orightml");
                            delete gameState.State.Cooldowns[selector];
                            return;
                        }
                        $el.html(origHtml + " (" + cooldown + ")");
                    });
                }
            }, 1000);
        };
        Cooldowns.Set = function Set(selector, duration) {
            if (typeof duration === "undefined") { duration = 5; }
            this._gameState.State.Cooldowns[selector] = duration;
            this.Initialize(selector);
        };
        Cooldowns.SetIfGreater = function SetIfGreater(selector, duration) {
            if (typeof duration === "undefined") { duration = 5; }
            var cd = this._gameState.State.Cooldowns[selector];
            if(cd == null || duration > cd) {
                this._gameState.State.Cooldowns[selector] = duration;
            }
            if(cd == null) {
                this.Initialize(selector);
            }
        };
        Cooldowns.Increase = function Increase(selector, duration) {
            if (typeof duration === "undefined") { duration = 5; }
            var cd = this._gameState.State.Cooldowns[selector];
            this._gameState.State.Cooldowns[selector] = (cd == null ? duration : (cd + duration));
            if(cd == null) {
                this.Initialize(selector);
            }
        };
        Cooldowns.Decrease = function Decrease(selector, duration) {
            if (typeof duration === "undefined") { duration = 5; }
            var cd = this._gameState.State.Cooldowns[selector];
            this._gameState.State.Cooldowns[selector] = (cd == null ? 0 : (Math.max(cd - duration, 0)));
            if(cd == null) {
                this.Initialize(selector);
            }
        };
        Cooldowns.Lock = function Lock() {
            this._locked = true;
            $("button").attr("disabled", "disabled");
        };
        Cooldowns.Unlock = function Unlock() {
            if(!this._locked) {
                return;
            }
            this._locked = false;
            $("button").removeAttr("disabled");
            for(var selector in this._gameState.State.Cooldowns) {
                this.Initialize(selector);
            }
        };
        Cooldowns.Initialize = function Initialize(selector) {
            var me = this;
            $(selector).each(function () {
                var $el = $(this).attr("disabled", "disabled");
                var origHtml = $el.attr("data-orightml");
                if(origHtml == null) {
                    $el.attr("data-orightml", origHtml = $el.html());
                }
                if(!me._locked) {
                    $el.html(origHtml + " (" + me._gameState.State.Cooldowns[selector] + ")");
                }
            });
        };
        return Cooldowns;
    })();
    ScrambledEggs.Cooldowns = Cooldowns;    
})(ScrambledEggs || (ScrambledEggs = {}));
